Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

176
Views
With javascript, please tell me how to get result array like below "resultList". It is similar to the UPDATE statement in SQL

With javascript, please tell me how to get result array like below "resultList". It is similar to the UPDATE statement in SQL.

const dataList = [
  { id: 1, xpstn: "10", ypstn: "11", value:{1,2,3,4}},
  { id: 2, xpstn: "20", ypstn: "21", value:{1,2,3,4}},
  { id: 3, xpstn: "30", ypstn: "31", value:{1,2,3,4}},
  { id: 4, xpstn: "40", ypstn: "41", value:{1,2,3,4}},
     ]
 const paramsList = [
  { id: 3, xpstn: "33", ypstn: "32", desc: "param"},
  { id: 4, xpstn: "44", ypstn: "42", desc: "param"},
  { id: 5, xpstn: "55", ypstn: "53", desc: "param"},
  { id: 6, xpstn: "66", ypstn: "62", desc: "param"},
];


let resultList = [
  { id: 3, xpstn: "33", ypstn: "11", value:{1,2,3,4}},
  { id: 4, xpstn: "44", ypstn: "21", value:{1,2,3,4}},
];

The condition is that the id is the same, and xpstn must be updated. /* UPDATE X SET X.xpstn = Y.xpstn FROM dataList X INNER JOIN paramsList Y
ON X.id = Y.id */

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

hope this works for you.

const dataList = [
    { id: 1, xpstn: "10", ypstn: "11", value: [1, 2, 3, 4] },
    { id: 2, xpstn: "20", ypstn: "21", value: [1, 2, 3, 4] },
    { id: 3, xpstn: "30", ypstn: "31", value: [1, 2, 3, 4] },
    { id: 4, xpstn: "40", ypstn: "41", value: [1, 2, 3, 4] },
]
const paramsList = [
    { id: 3, xpstn: "33", ypstn: "32", desc: "param" },
    { id: 4, xpstn: "44", ypstn: "42", desc: "param" },
    { id: 5, xpstn: "55", ypstn: "53", desc: "param" },
    { id: 6, xpstn: "66", ypstn: "62", desc: "param" },
];


let resultList = [
    { id: 3, xpstn: "33", ypstn: "31", value: [1, 2, 3, 4] },
    { id: 4, xpstn: "44", ypstn: "41", value: [1, 2, 3, 4] },
];

/*
UPDATE X
SET X.xpstn = Y.xpstn
FROM dataList X
INNER JOIN paramsList Y  
ON X.id = Y.id
*/

const update = (data = [], params = []) => {
    resultList = [];
    data = data.forEach(d => {
        if (params.some(p => p.id === d.id)) {
            r = {
                ...d,
                xpstn: params.find(p => p.id === d.id).xpstn
            }
            resultList.push(r);
        }
    });
    return resultList;
}

console.log("result", update(dataList, paramsList));

about 4 years ago · Juan Pablo Isaza Report

0

Solution with reduce:

const dataList = [{ id: 1, xpstn: "10", ypstn: "11", value: [1, 2, 3, 4] },{ id: 2, xpstn: "20", ypstn: "21", value: [1, 2, 3, 4] },{ id: 3, xpstn: "30", ypstn: "31", value: [1, 2, 3, 4] },{ id: 4, xpstn: "40", ypstn: "41", value: [1, 2, 3, 4] }];
const paramsList = [{ id: 3, xpstn: "33", ypstn: "32", desc: "param" },{ id: 4, xpstn: "44", ypstn: "42", desc: "param" },{ id: 5, xpstn: "55", ypstn: "53", desc: "param" },{ id: 6, xpstn: "66", ypstn: "62", desc: "param" }];

const resultList = dataList.reduce((acc, dataItem) => {
    const paramItem = paramsList.find(({ id }) => id === dataItem.id);
    if (paramItem) {
        const { xpstn } = paramItem;
        acc.push({ ...dataItem,  xpstn });
    }
    return acc;
}, []);

console.log(resultList)
.as-console-wrapper { max-height: 100% !important; top: 0 }

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!